package net.mcreator.infinite_storage.procedure;

import org.lwjgl.input.Keyboard;

import net.minecraft.world.World;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.math.BlockPos;
import net.minecraft.server.MinecraftServer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.item.ItemStack;
import net.minecraft.inventory.Slot;
import net.minecraft.inventory.Container;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.Entity;
import net.minecraft.command.ICommandSender;

import net.mcreator.infinite_storage.ElementsInfiniteStorageMod;

import java.util.function.Supplier;
import java.util.Map;
import java.util.HashMap;

@ElementsInfiniteStorageMod.ModElement.Tag
public class ProcedureSamllBagGuiUpdate extends ElementsInfiniteStorageMod.ModElement {
	public ProcedureSamllBagGuiUpdate(ElementsInfiniteStorageMod instance) {
		super(instance, 63);
	}
	static Map<String, Object> $_dependencies = new HashMap<>();
	public static void executeProcedure(Map<String, Object> dependencies) {
		if (dependencies.get("entity") == null) {
			return;
		}
		Entity entity = (Entity) dependencies.get("entity");
		String IndexTextTemp = "";
		String IndexTextTemp1 = "";
		String IndexTextTemp2 = "";
		String IndexTextTemp3 = "";
		String ItemIdTemp = "";
		String ItemIdTemp2 = "";
		NBTTagCompound InfiniteBag = entity.getEntityData().getCompoundTag("InfiniteBag");
		if (Keyboard.isKeyDown(Keyboard.KEY_R)) {
			if ((!InfiniteBag.getBoolean("ItemReset"))) {
				InfiniteBag.setBoolean("ItemReset", (true));
				{
					Entity _ent = entity;
					if (!_ent.world.isRemote && _ent.world.getMinecraftServer() != null) {
						_ent.world.getMinecraftServer().getCommandManager().executeCommand(new ICommandSender() {
							@Override
							public String getName() {
								return "";
							}

							@Override
							public boolean canUseCommand(int permission, String command) {
								return true;
							}

							@Override
							public World getEntityWorld() {
								return _ent.world;
							}

							@Override
							public MinecraftServer getServer() {
								return _ent.world.getMinecraftServer();
							}

							@Override
							public boolean sendCommandFeedback() {
								return false;
							}

							@Override
							public BlockPos getPosition() {
								return _ent.getPosition();
							}

							@Override
							public Vec3d getPositionVector() {
								return new Vec3d(_ent.posX, _ent.posY, _ent.posZ);
							}

							@Override
							public Entity getCommandSenderEntity() {
								return _ent;
							}
						}, "playsound minecraft:ui.button.click player @s ~ ~ ~");
					}
				}
				{
					$_dependencies.put("entity", entity);
					ProcedureSamllBagGuiClose.executeProcedure($_dependencies);
				}
				String sortTextTemp = (((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getHeldItemMainhand() : ItemStack.EMPTY)
						.hasTagCompound()
								? ((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getHeldItemMainhand() : ItemStack.EMPTY)
										.getTagCompound().getString("ItemSave")
								: "");
				String[] sortText = sortTextTemp.split("_,_");
				String sortText1Temp = (((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getHeldItemMainhand() : ItemStack.EMPTY)
						.hasTagCompound()
								? ((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getHeldItemMainhand() : ItemStack.EMPTY)
										.getTagCompound().getString("ItemNumSave")
								: "");
				String[] sortText1 = sortText1Temp.split("_,_");
				String sortText2Temp = (((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getHeldItemMainhand() : ItemStack.EMPTY)
						.hasTagCompound()
								? ((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getHeldItemMainhand() : ItemStack.EMPTY)
										.getTagCompound().getString("ItemMetaSave")
								: "");
				String[] sortText2 = sortText2Temp.split("_,_");
				String sortText3Temp = (((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getHeldItemMainhand() : ItemStack.EMPTY)
						.hasTagCompound()
								? ((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getHeldItemMainhand() : ItemStack.EMPTY)
										.getTagCompound().getString("ItemNbtSave")
								: "");
				String[] sortText3 = sortText3Temp.split("_,_");
				for (int i = 0; i < sortText.length; i++) {
					if ((!((sortText[i]).equals("minecraft:air")))) {
						ItemIdTemp2 = (String) ((sortText[i]) + "" + ("-") + "" + (sortText2[i]));
						if ((!((ItemIdTemp).contains((ItemIdTemp2))))) {
							ItemIdTemp = (String) (((ItemIdTemp)) + "" + ((ItemIdTemp2)) + "" + (","));
							int ItemNumTemp = new Object() {
								int convert(String s) {
									try {
										return Integer.parseInt(s.trim());
									} catch (Exception e) {
									}
									return 0;
								}
							}.convert(sortText1[i]);
							for (int j = (i + 1); j < sortText.length; j++) {
								if ((((sortText[i]).equals(sortText[j]))
										&& (((sortText2[i]).equals(sortText2[j])) && ((sortText3[i]).equals(sortText3[j]))))) {
									ItemNumTemp = (ItemNumTemp + new Object() {
										int convert(String s) {
											try {
												return Integer.parseInt(s.trim());
											} catch (Exception e) {
											}
											return 0;
										}
									}.convert(sortText1[j]));
								}
							}
							int NumC = (((new Object() {
								public ItemStack getItemStack(int sltid) {
									if (entity instanceof EntityPlayerMP) {
										Container _current = ((EntityPlayerMP) entity).openContainer;
										if (_current instanceof Supplier) {
											Object invobj = ((Supplier) _current).get();
											if (invobj instanceof Map) {
												return ((Slot) ((Map) invobj).get(sltid)).getStack();
											}
										}
									}
									return ItemStack.EMPTY;
								}
							}.getItemStack((int) (i)))).getMaxStackSize());
							int NumA = (ItemNumTemp / NumC);
							int NumB = (ItemNumTemp % NumC);
							for (int j = i; j < sortText.length; j++) {
								if ((((sortText[i]).equals(sortText[j]))
										&& (((sortText2[i]).equals(sortText2[j])) && ((sortText3[i]).equals(sortText3[j]))))) {
									if ((NumA > 0)) {
										sortText1[j] = (new java.text.DecimalFormat("0").format(NumC));
										NumA = (NumA - 1);
									} else if ((NumB > 0)) {
										sortText1[j] = (new java.text.DecimalFormat("0").format(NumB));
										NumB = 0;
									} else {
										sortText[j] = "minecraft:air";
										sortText1[j] = "0";
										sortText2[j] = "0";
										sortText3[j] = "-1";
									}
								}
							}
						}
					}
				}
				for (int i = 0; i < sortText.length; i++) {
					int minIndex = i;
					for (int j = (i + 1); j < sortText.length; j++) {
						if (((sortText[j]).equals("minecraft:air"))) {
							continue;
						}
						ItemIdTemp = (String) ((sortText[j]) + "" + ("-") + "" + (sortText2[j]));
						ItemIdTemp2 = (String) ((sortText[minIndex]) + "" + ("-") + "" + (sortText2[minIndex]));
						if ((((sortText[minIndex]).equals("minecraft:air")) || (((ItemIdTemp)).compareTo((ItemIdTemp2)) < 0))) {
							minIndex = j;
						}
					}
					IndexTextTemp = (String) sortText[minIndex];
					IndexTextTemp1 = (String) sortText1[minIndex];
					IndexTextTemp2 = (String) sortText2[minIndex];
					IndexTextTemp3 = (String) sortText3[minIndex];
					sortText[minIndex] = sortText[i];
					sortText1[minIndex] = sortText1[i];
					sortText2[minIndex] = sortText2[i];
					sortText3[minIndex] = sortText3[i];
					sortText[i] = (IndexTextTemp);
					sortText1[i] = (IndexTextTemp1);
					sortText2[i] = (IndexTextTemp2);
					sortText3[i] = (IndexTextTemp3);
				}
				for (int i = 0; i < sortText.length; i++) {
					if (entity instanceof EntityPlayerMP) {
						Container _current = ((EntityPlayerMP) entity).openContainer;
						if (_current instanceof Supplier) {
							Object invobj = ((Supplier) _current).get();
							if (invobj instanceof Map) {
								ItemStack _setstack = (ItemStack.EMPTY);
								_setstack.setCount((int) 1);
								((Slot) ((Map) invobj).get((int) (i))).putStack(_setstack);
								_current.detectAndSendChanges();
							}
						}
					}
				}
				{
					ItemStack _stack = ((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getHeldItemMainhand() : ItemStack.EMPTY);
					if (!_stack.hasTagCompound())
						_stack.setTagCompound(new NBTTagCompound());
					_stack.getTagCompound().setString("ItemSave", String.join(("_,_"), sortText));
				}
				{
					ItemStack _stack = ((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getHeldItemMainhand() : ItemStack.EMPTY);
					if (!_stack.hasTagCompound())
						_stack.setTagCompound(new NBTTagCompound());
					_stack.getTagCompound().setString("ItemNumSave", String.join(("_,_"), sortText1));
				}
				{
					ItemStack _stack = ((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getHeldItemMainhand() : ItemStack.EMPTY);
					if (!_stack.hasTagCompound())
						_stack.setTagCompound(new NBTTagCompound());
					_stack.getTagCompound().setString("ItemMetaSave", String.join(("_,_"), sortText2));
				}
				{
					ItemStack _stack = ((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getHeldItemMainhand() : ItemStack.EMPTY);
					if (!_stack.hasTagCompound())
						_stack.setTagCompound(new NBTTagCompound());
					_stack.getTagCompound().setString("ItemNbtSave", String.join(("_,_"), sortText3));
				}
				{
					$_dependencies.put("entity", entity);
					ProcedureSmallBagGuiOpen.executeProcedure($_dependencies);
				}
			}
		} else if (InfiniteBag.getBoolean("ItemReset")) {
			InfiniteBag.setBoolean("ItemReset", (false));
		}
		entity.getEntityData().setTag("InfiniteBag", InfiniteBag);
	}
}
